package cd.value.framework;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import com.google.gson.Gson;

public class Mongodb {
	private Gson gson;
	private Document doc;
	public Mongodb(){
		this.doc = new Document();
		this.gson = new Gson();
	}
	public Mongodb(String path) throws IOException, JDOMException{
		FileInputStream in = new FileInputStream(path);
		this.doc = new SAXBuilder().build(in);
	}
	/**
	 * 
	 * @param path 给个xml路径创建数据库
	 * 初始化有三张表 test用于存放测试数据
	 * sys_user 
	 * @throws IOException
	 */
	public void createDatabase(String path) throws IOException{
		FileOutputStream out = new FileOutputStream(path);
		//创建根节点
		Element root = new Element("dataBase");
		Element table =new Element("table");
		table.setAttribute("name","sys_test");
		root.addContent(table);
		
		this.doc.addContent(root);
		XMLOutputter xmlout = new XMLOutputter();
		xmlout.output(this.doc,out);
		System.out.println("-----建库成功-------");
	}
	/**
	 * 
	 * @param path 给个xml路径创建数据库
	 * 初始化有三张表 test用于存放测试数据
	 * sys_user 
	 * @throws JDOMException 
	 * @throws IOException
	 */
	public void addData(String path,String tablename, Object po) throws JDOMException, IOException{
		FileInputStream in = new FileInputStream(path);
		this.doc = new SAXBuilder().build(in);
		Element root = this.doc.getRootElement();
		List<Element> list = root.getChildren();
		for (Element element : list) {
			System.out.println(element.getAttributeValue("name"));
			if(element.getAttributeValue("name").equals(tablename)){
				Element data = new Element("data");
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("_uuid",UUID.randomUUID().toString());
				String json = this.gson.toJson(po);
				Map<String,Object> gsonMap = this.gson.fromJson(json, Map.class);
				map.putAll(gsonMap);
				data.addContent(gson.toJson(map));
				element.addContent(data);
			}
			
		}
		XMLOutputter out = new XMLOutputter();
		out.output(this.doc, new FileOutputStream(path));
		System.out.println("----添加数据成功-------");
		
	}
	public void updateData(String path,String tablename, Object po)throws JDOMException, IOException{
		FileInputStream in = new FileInputStream(path);
		this.doc = new SAXBuilder().build(in);
		Element root = this.doc.getRootElement();
		List<Element> list = root.getChildren();
		for (Element element : list) {
			if(element.getAttributeValue("name").equals(tablename)){
				List<Element> els = element.getChildren();
				for(Element e :els){
					Map map = this.gson.fromJson(element.getText(),Map.class);
					Map newMap = this.gson.fromJson(this.gson.toJson(po),Map.class);
					if(map.get("_uuid").equals(newMap.get("_uuid"))){
						map.putAll(newMap);
						Element data = new Element("data");
						data.setText(gson.toJson(map));
						element.removeContent(e);
						element.addContent(data);
						break;
					}
				}
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.output(this.doc, new FileOutputStream(path));
		System.out.print("---修改成功-----");
	}
	public void deleteData(String path, String tablename, Object po)
			throws JDOMException, IOException {
		FileInputStream in = new FileInputStream(path);
		this.doc = new SAXBuilder().build(in);
		Element root = this.doc.getRootElement();
		List<Element> list = root.getChildren();
		for (Element element : list) {
			if(element.getAttributeValue("name").equals(tablename)){
				List<Element> chidens = element.getChildren();
				for (Element e : chidens) {
					Map<String,Object> poMap = this.gson.fromJson(gson.toJson(po), Map.class);
					Map<String,Object> map = gson.fromJson(e.getText(), Map.class);
					if(map.get("_uuid").equals(poMap.get("_uuid"))){
						element.removeContent(e);
					}
				}
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.output(this.doc, new FileOutputStream(path));
		System.out.print("---修改成功-----");
	}
	public List<Map> findAll(String path, String tablename)
			throws Exception {
		FileInputStream in = new FileInputStream(path);
		this.doc = new SAXBuilder().build(in);
		Element root = this.doc.getRootElement();
		List<Map> ll = new ArrayList<Map>();
		List<Element> els = root.getChildren();
		for (Element element : els) {
			List<Element> datalist = element.getChildren();
			for (Element data : datalist) {
				String dataText = data.getText();
				Map<String,Object> map = gson.fromJson(dataText,Map.class);
				ll.add(map);
			}
		}
		return ll;
	}
	public Gson getGson() {
		return gson;
	}
	public void setGson(Gson gson) {
		this.gson = gson;
	}
	public Document getDoc() {
		return doc;
	}
	public void setDoc(Document doc) {
		this.doc = doc;
	}
	
	
}
